package com.payroll.taglib.html;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.struts.util.ResponseUtils;


public class HeadTag extends BodyTagSupport {


	private String contextRoot = null;

	//Attributes
	private String title = "";
	private String validation = "false";
	private String tab = "false";
	private String pagescript;
	private String cache;

	public String getPagescript()
	{
		return this.pagescript;
	}

	public void setPagescript(String p_Pagescript)
	{
		this.pagescript = p_Pagescript;
	}

	public String getTitle()
	{
		return this.title;
	}

	public void setTitle(String p_Title)
	{
		this.title = p_Title;
	}

	/**
	 * Constructor for HeadTag
	 */
	public HeadTag() {

		super();

	}


	private void getAppHeaderInfo(StringBuffer p_BeginHeader)
	{
		//Title pulled from attribute
		p_BeginHeader.append("<title>" + this.getTitle() + "</title>\n");

        //Encoding
        p_BeginHeader.append("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n");

		//Sacwis style sheet
		p_BeginHeader.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + this.contextRoot + "common/sacwis.css\">\n");

		//Master.js
		p_BeginHeader.append("<script type=\"text/JavaScript\" language=\"JavaScript\" src=\"" + this.contextRoot + "scripts/common/Master.js\"></script> \n");

		//Javascript context root variable
		p_BeginHeader.append(
                "<script type=\"text/JavaScript\" language=\"JavaScript\">\n" +
                "  var contextRoot = '" + this.contextRoot + "';\n" +
				"  var callbackMethodName=\"continue\";\n" +
				"</script> \n");



		/*if(!Foundation.getProperty("developerMode").equals("on")) {
			p_BeginHeader.append("<SCRIPT type=\"text/JavaScript\" LANGUAGE=\"JavaScript\" SRC=\"" + this.contextRoot + "scripts/common/BrowserDisable.js\"></SCRIPT> \n");
		}*/


	}

	private void getOptHeaderInfo(StringBuffer p_BeginHeader)
	{
		//validation.js
		if(getValidation().equals("true")) {
			p_BeginHeader.append("<SCRIPT type=\"text/JavaScript\" LANGUAGE=\"JavaScript\" SRC=\"" + this.contextRoot + "scripts/common/Validation.js\"></SCRIPT> \n");
			p_BeginHeader.append("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"" + this.contextRoot + "common/calendar-win2k-1.css\" title=\"win2k-1\" />\n");
			p_BeginHeader.append("<script type=\"text/JavaScript\" src=\"" + this.contextRoot + "scripts/common/calendar.js\"></script>\n");
			p_BeginHeader.append("<script type=\"text/JavaScript\" src=\"" + this.contextRoot + "scripts/common/cal-sup.js\"></script>\n");
			p_BeginHeader.append("<script type=\"text/JavaScript\" src=\"" + this.contextRoot + "scripts/common/lang/calendar-en.js\"></script>\n");

		}
		//tabclick.js
		if(getTab().equals("true"))
			p_BeginHeader.append("<SCRIPT type=\"text/JavaScript\" LANGUAGE=\"JavaScript\" SRC=\"" + this.contextRoot + "scripts/common/TabClick.js\"></SCRIPT> \n");

		//Page specific script
		if(getPagescript() != null)
		{
			p_BeginHeader.append("<SCRIPT type=\"text/JavaScript\" LANGUAGE=\"JavaScript\" SRC=\"" + this.contextRoot + this.pagescript + "\"></SCRIPT> \n");
		}

	}


	/**
     * Process the beginning of this tag.
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {

    	//set context root
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		this.contextRoot = request.getContextPath() + "/";

    	StringBuffer m_BeginHeader = new StringBuffer();


    	//Begin head tag
    	m_BeginHeader.append("<HEAD> \n");

    	//Get the required header information
    	getAppHeaderInfo(m_BeginHeader);

	    //Get optional header info
	    getOptHeaderInfo(m_BeginHeader);

	    // Render this element to our writer
        ResponseUtils.write(pageContext, m_BeginHeader.toString());

		// Evaluate the remainder of this page
        return EVAL_PAGE;
    }

    public int doEndTag() throws JspException {

    	StringBuffer m_EndHeader = new StringBuffer();

    	//Print anything in the body of the tag
    	BodyContent m_Body = getBodyContent();
    	if(m_Body != null)
	    	m_EndHeader.append(m_Body.getString() + "\n");

    	if (cache != null && cache.equalsIgnoreCase("true")){
			m_EndHeader.append("<meta http-equiv=\"Cache-Control\" content=\"public,pre-check=86400,post-check=86400\"/>\n");
			m_EndHeader.append("<meta http-equiv=\"Expires\" content=\"Thu, 1 Jan 2099 00:00:00 GMT\"/>\n");
    	}

    	//End head tag
	    m_EndHeader.append("</HEAD> \n");

	    ResponseUtils.write(pageContext, m_EndHeader.toString());

    	return EVAL_PAGE;
    }

    /**
     * Release any acquired resources.
     */
    public void release() {


		super.release();
		contextRoot = null;
		title = null;
		pagescript = null;
		tab = null;
		validation = null;
		cache = null;

    }
 	/**
	 * Gets the tab
	 * @return Returns a String
	 */
	public String getTab() {
		return tab;
	}
	/**
	 * Sets the tab
	 * @param tab The tab to set
	 */
	public void setTab(String tab) {

		if(tab == null) {
			this.tab = "false";
		}
		else {
			this.tab = tab;
		}
	}


	/**
	 * Gets the validation
	 * @return Returns a String
	 */
	public String getValidation() {
		return validation;
	}
	/**
	 * Sets the validation
	 * @param validation The validation to set
	 */
	public void setValidation(String validation) {

		if(validation == null) {
			this.validation = "false";
		}
		else {
			this.validation = validation;
		}
	}


	/**
	 * Returns the cache.
	 * @return String
	 */
	public String getCache() {
		return cache;
	}

	/**
	 * Sets the cache.
	 * @param cache The cache to set
	 */
	public void setCache(String cache) {
		this.cache = cache;
	}

}
